package cn.duli.xianer.controller;


import cn.duli.xianer.pojo.Follow;
import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.Users;
import cn.duli.xianer.service.FollowService;
import cn.duli.xianer.service.UsersService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    @Autowired
    private UsersService usersService;

    /**
     * 新增关注
     * @param follow
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult save(Follow follow) {
        int fId = followService.save(follow);
        return  new MyResult(200, "新增关注成功",null,null,fId,null);
    }

    /**
     * 取消关注（根据id删除此关注）
     * @param fId
     */
    @RequestMapping("/deleteById/{fId}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult deleteById(@PathVariable("fId") Integer fId) {
        followService.deleteById(fId);
        MyResult myResult = new MyResult();
        myResult.setCode(200);
        myResult.setMsg("取消关注成功");
        return myResult;
    }

    /**
     * 根据当前登录用户id查看关注的用户列表
     * @param fCurrentUid
     * @return
     */
    @GetMapping("/findByCurrId/{fCurrentUid}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findByCurrId(@PathVariable("fCurrentUid") Integer fCurrentUid) {
        List<Follow> list = followService.findByCurrId(fCurrentUid);
        MyResult myResult = new MyResult();
        myResult.setCode(200);
        myResult.setMsg("根据当前登录用户id查看关注的用户列表成功");
        myResult.setList(list);
        return myResult;
    }

    /**
     * 根据关注用户id查找粉丝列表
     * @param fUid
     * @return
     */
    @GetMapping("/findByUserId/{fUid}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business"})
    public MyResult findByUserId(@PathVariable("fUid") Integer fUid) {
        List<Follow> list = followService.findByUserId(fUid);
        List<Users> newList = new ArrayList<>();
        for (int i=0;i<list.size();i++) {
            newList.add(usersService.findById(list.get(i).getfCurrentUid()));
        }
        MyResult myResult = new MyResult();
        myResult.setCode(200);
        myResult.setMsg("根据关注用户id查找粉丝列表成功");
        myResult.setList(newList);
        return myResult;
    }

    /**
     * 根据当前登录用户id查看关注的用户数
     * @param fCurrentUid
     * @return
     */
    @RequestMapping("/findFollowCount/{fCurrentUid}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findFollowCount(@PathVariable("fCurrentUid") Integer fCurrentUid) {
        int count = followService.findFollowCount(fCurrentUid);
        return new MyResult(200, "根据当前登录用户id查看关注的用户数",null,null,count,null);
    }

    /**
     * 根据关注用户id查看粉丝的用户数
     * @param fUid
     * @return
     */
    @RequestMapping("/findFansCount/{fUid}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findFansCount(@PathVariable("fUid") Integer fUid) {
        int count = followService.findFansCount(fUid);
        return new MyResult(200, "根据关注用户id查看粉丝的用户数",null,null,count,null);
    }

}
